package view;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.*;

import util.resources.ResourceManager;

/**
 * 
 * Creates a panel holding a list of variables and a history.
 * Variables are specific to each workspace while history is
 * shared among all workspaces.
 * 
 * @author wesleybrown
 *
 */
public class InfoPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
	private DefaultListModel dVar;
	private DefaultListModel dHist;

	/**
	 * 
	 * Constructs a panel containing a history and variable list.
	 * 
	 * @param resources ResourceManager linked to property file
	 * including settings for history and variable panes.
	 */
	public InfoPanel (ResourceManager resources) {
			
		int[] panelSize = resources.getIntegerArray("infoPanelSize");
		this.setPreferredSize(new Dimension(panelSize[0], panelSize[1]));
		this.setLayout(new GridLayout(1, 1));
		splitPane.setDividerLocation(resources.getInteger("dividerLoc"));
		
		// Variable List
		dVar = new DefaultListModel();
		JList varList = new JList(dVar);
		varList.setBorder(BorderFactory.createTitledBorder("Variables"));
		
		// History List
		dHist = new DefaultListModel();
		JList histList = new JList(dHist);
		histList.setBorder(BorderFactory.createTitledBorder("History"));
		//TODO: add listener to that
		
		splitPane.setTopComponent(varList);
		splitPane.setBottomComponent(histList);
		
		this.add(splitPane);
	}
	
	// Should these be via a map or a list of strings?
	
	/**
	 * Removes all variables stored in variable list.
	 */
	public void removeAllVariables ()
	{
		dVar.clear();
	}

	/**
	 * Removes variable stored in variable list at a specified
	 * postion.
	 * 
	 * @param i Position of variable
	 */
	public void removeVariable (int i)
	{
		dVar.remove(i);
	}

	/**
	 * Adds a variable to the variable list.
	 * 
	 * @param s Variable to be added.
	 */
	public void addVariable(String msg){
		dVar.addElement(msg);
	}
	
	//add history
	public void addHistory(String s)
	{
		dHist.addElement(s);
	}
	public void cleanAndUpdate(List<String> list) {
		removeAllVariables();
		for (String s : list)
			addVariable(s);
	}
}
